﻿/**
 * @author sminutoli
 * version 1.0
 * relaciona 
 */
import com.dinmax.flash.utilities.date.DateUtils
import com.dinmax.flash.central.extension.message.Message

class com.dinmax.flash.central.bridge.VOICEMAILDetailBridge
extends MovieClip {
	
	private static var ENABLED:String = "enabled";
	private static var DISABLED:String = "disabled";
	
	private var mcClose:MovieClip;
	private var mcBack:MovieClip;
	private var mcPlay:MovieClip;
	private var mcStop:MovieClip;
	private var txt_date:TextField;
	private var txt_from:TextField;
	private var txt_bytes:TextField;
	private var sVoicemail:Sound;
	
	private var listener:Object;
	private var value:Object;
	
	private var readMessage:Boolean;
	
	public function VOICEMAILDetailBridge() {
		//trace("new VOICEMAILDetailBridge()");
		
		this.readMessage=false;
		
		var root:VOICEMAILDetailBridge = this;
		
		this.sVoicemail = new Sound(this);
		
		this.mcClose.onPress= function():Void {
			root.listener.closeDisplay(root.getValue());
		};
		this.mcBack.onPress= function():Void {
			root.listener.closeDisplay();
		};
		
		this.setState(VOICEMAILDetailBridge.DISABLED, this.mcPlay);
		this.setState(VOICEMAILDetailBridge.DISABLED, this.mcStop);
		
		this.sVoicemail.onLoad = function ():Void {
			this.stop();
			root.txt_bytes.text = "sound loaded "+DateUtils.milisecondsToString(this.duration);
			root.setState(VOICEMAILDetailBridge.ENABLED, root.mcPlay);
			return;
		};
		this.sVoicemail.onSoundComplete= function ():Void {
			root.sVoicemail.start(0);
			root.sVoicemail.stop();
			root.setState(VOICEMAILDetailBridge.DISABLED, root.mcStop);
		};
						
		this.mcPlay.onPress= function():Void {
			root.sVoicemail.start();
			root.updateSoundProgress();
			if(!root.readMessage) root.listener.refreshVoiceMessages();
			root.setState(VOICEMAILDetailBridge.ENABLED, root.mcStop);
			return;
		};
		this.mcStop.onPress= function():Void {
			root.sVoicemail.start(0);
			root.sVoicemail.stop();
			root.updateSoundProgress(true);
			root.setState(VOICEMAILDetailBridge.DISABLED, this);
		};
		
	};
	private function updateSoundProgress(terminate:Boolean):Void {
		var clip:MovieClip = this.createEmptyMovieClip("mcCheck", this.getNextHighestDepth());
		var root:VOICEMAILDetailBridge = this;
		clip.onEnterFrame= function():Void {
			if(root.sVoicemail.position == root.sVoicemail.duration || terminate){
				root.txt_bytes.text = DateUtils.milisecondsToString(root.sVoicemail.position) +" de "+DateUtils.milisecondsToString(root.sVoicemail.duration);
				delete this.onEnterFrame;
				removeMovieClip(this);
			} else {
				root.txt_bytes.text = DateUtils.milisecondsToString(root.sVoicemail.position) +" de "+DateUtils.milisecondsToString(root.sVoicemail.duration);
			};
		};
		return;
	};
	
	private function setState(type:String, clip:MovieClip):Void {
		switch(type){
			case VOICEMAILDetailBridge.ENABLED:
			clip.enabled = true;
			clip._alpha = 100;
			break;
			
			case VOICEMAILDetailBridge.DISABLED:
			clip.enabled = false;
			clip._alpha = 50;
			break;
		};
		return;
	};
	
	public function display(time:Date, from:String):Void{
		//trace("VOICEMAILDetailBridge.as display()");
		this.setDate(DateUtils.dateToString(time, DateUtils.date_DD_MM_AA, "-"));
		this.setFrom(from);
		return;
	};
	
	public function setDate(date:String):Void {
		txt_date.text = date;
		return;
	};
	
	public function setFrom(from:String):Void {
		txt_from.text = from;
		return;
	};
	
	public function setListener(listener:Object):Void {
		this.listener = listener;
	}
	public function setValue(value:Object):Void {
		//trace("VOICEMAILDetailBridge.as setValue");
		this.value = value;
		if(value.getState() != Message.NEW){
			this.readMessage = true;
		};
		this.sVoicemail.loadSound(value.getMessageURL(), true);
		return;
	};
	public function getValue():Object {
		return this.value;
	};

}